package com.example.yuekaoceshi.adapter;


import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yuekaoceshi.R;
import com.example.yuekaoceshi.bean.Bean;
import com.example.yuekaoceshi.bean.Info;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 段傅华 on 2018/12/11.
 */

public class RlvAdapter2 extends RecyclerView.Adapter {
    private final ArrayList<Info> mList;
    private OnItemClickListener mListener;
    private OnItemClickListener mListener1;

    public RlvAdapter2(ArrayList<Info> list) {
        this.mList=list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlayout, null, false);
        MyViewHolder myViewHolder = new MyViewHolder(inflate);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder holder1= (MyViewHolder) holder;
        holder1.mTv.setText(mList.get(position).getName());
        holder1.mIv.setImageResource(mList.get(position).getUrl());
        holder1.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.OnItemClick(v,position);
            }
        });
        holder1.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mListener1.OnItemClick(v,position);
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addData(List<Info> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        final ImageView mIv;
        final TextView mTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            mIv = itemView.findViewById(R.id.item_iv);
            mTv = itemView.findViewById(R.id.item_tv);
        }
    }
    //点击监听
    public interface OnItemClickListener{
        void OnItemClick(View v,int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mListener=listener;
    }
    //长按监听
    public interface OnItemLongClickListener{
        void OnItemLongClick(View v,int position);
    }
    public void SetOnItemLongClickListener(OnItemClickListener listener){
        this.mListener1=listener;
    }
}
